package cn.lixufeng.user.controller;

import cn.lixufeng.basic.utils.AjaxResult;
import cn.lixufeng.basic.utils.PageList;
import cn.lixufeng.user.domain.LoginInfo;
import cn.lixufeng.user.query.LoginInfoQuery;
import cn.lixufeng.user.service.ILoginInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/logininfo")
@Api(tags = "登录信息接口",description = "登录信息接口详细描述")
public class LoginInfoController {

    @Autowired
    private ILoginInfoService loginInfoService;

    /**
     * 增加或修改
     * @param loginInfo
     * @return
     */
    @ApiOperation(value = "登录信息添加或修改",notes = "如果有id就是修改否则就是添加")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody LoginInfo loginInfo){
        try {
            if (loginInfo.getId() == null) {
                loginInfoService.add(loginInfo);
            } else {
                loginInfoService.update(loginInfo);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败");
        }
    }

    /**
     * 按id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            loginInfoService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败");
        }
    }

    /**
     * 按id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public LoginInfo findById(@PathVariable("id") Long id){
        return loginInfoService.findById(id);
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    public List<LoginInfo> findAll(){
        return loginInfoService.findAll();
    }

    /**
     * 分页和高级查询
     * @param loginInfoQuery
     * @return
     */
    @PostMapping
    public PageList<LoginInfo> findPage(@RequestBody LoginInfoQuery loginInfoQuery){
        return loginInfoService.findPage(loginInfoQuery);
    }

    /**
     * 批量删除
     * @param ids 是需要删除的id的集合，前台传的是数组，后台可以用数组或者集合来接收
     * @return
     */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            loginInfoService.batchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败");
        }
    }
}
